package com.xiaoyu.im.datamodel.exception;

public class IMException extends Exception {

    public final int code;

    public IMException() {
        this.code = 0;
    }

    public IMException(String message) {
        super(message);
        this.code = 0;
    }

    public IMException(String message, Throwable cause) {
        super(message, cause);
        this.code = getIMExceptionCode(cause);
    }

    public IMException(Throwable cause) {
        super(cause);
        this.code = getIMExceptionCode(cause);
    }

    public IMException(int code) {
        this.code = code;
    }

    public IMException(String message, int code) {
        super(message);
        this.code = code;
    }

    public IMException(String message, Throwable cause, int code) {
        super(message, cause);
        this.code = code;
    }

    public IMException(Throwable cause, int code) {
        super(cause);
        this.code = code;
    }

    public static int getIMExceptionCode(Throwable e) {
        return e instanceof IMException ? ((IMException) e).code : 0;
    }

    @Override
    public String toString() {
        return '{' + super.toString() + ", code: " + code + '}';
    }
}
